package com.bloveoacom.weight;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bloveoacom.weichi.R;


public class RemindDialog extends Dialog implements View.OnClickListener {
    private TextView tv_content,tv_title;
    private TextView btn_right, btn_left;
    private RelativeLayout rl_left, rl_right;
    private ItemClickListener itemClickListener;
    private String title,content, rightText, leftText;
    private View view_line;
    private boolean isShowLeft = true;
    //设置按钮内容
    public void setRightText(String rightText) {
        this.rightText = rightText;
        if (btn_right != null&&!TextUtils.isEmpty(rightText))
            btn_right.setText(rightText);
    }


    public void setLeftText(String leftText) {
        this.leftText = leftText;
        if (btn_left != null&&!TextUtils.isEmpty(leftText))
            btn_left.setText(leftText);
    }


    public void setContent(String content) {
        this.content = content;
        if (tv_content != null)
            tv_content.setText(content);
    }

    public void setTitle(String title) {
        this.title = title;
        if (tv_title != null){
            if(!TextUtils.isEmpty(title)){
                tv_title.setText(title);
                tv_title.setVisibility(View.VISIBLE);
            }else{
                tv_title.setVisibility(View.GONE);
            }
        }

    }

    /**
     * 只设置内容
     * @param context
     * @param content
     * @param theme
     */
    public RemindDialog(Context context, String content, int theme) {
        super(context, theme);
        this.content = content;
    }

    /**
     * 设置左右按钮字符 和中间内容字符
     * @param context
     * @param content
     * @param theme
     * @param lefttext
     * @param righttext
     */
    public RemindDialog(Context context, String title, String content, String lefttext, String righttext, int theme) {
        super(context, theme);
        this.title = title;
        this.content = content;
        this.leftText = lefttext;
        this.rightText = righttext;
    }

    public RemindDialog(Context context, String title, String content, String righttext, int theme) {
        super(context, theme);
        this.title = title;
        this.content = content;
        this.rightText = righttext;
        isShowLeft = false;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_public);
        this.setCanceledOnTouchOutside(true);
        tv_content = (TextView) findViewById(R.id.tv_content);
        btn_right = (TextView) findViewById(R.id.btn_right);
        btn_left = (TextView) findViewById(R.id.btn_left);
//        tv_title = (TextView) findViewById(R.id.tv_title);
        view_line = findViewById(R.id.view_line);
        rl_left = (RelativeLayout) findViewById(R.id.rl_left);
        rl_right = (RelativeLayout) findViewById(R.id.rl_right);

        //设置内容
        tv_content.setText(content);
        setLeftText(leftText);
        setRightText(rightText);

        //设置监听
        rl_right.setOnClickListener(this);
        rl_left.setOnClickListener(this);


        //设置标题以及是否隐藏
        setTitle(title);

        //隐藏显示处理
        if(!isShowLeft){
            setButtonSingle();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_right:
                if (itemClickListener != null) {
                    dismiss();
                    itemClickListener.right();
                }
                break;
            case R.id.rl_left:
                if (itemClickListener != null) {
                    dismiss();
                    itemClickListener.left();
                }
                break;
            default:
                break;
        }
    }

    public interface ItemClickListener {
        void left();

        void right();
    }

    public void setButtonSingle() {
        rl_left.setVisibility(View.GONE);
        rl_right.setBackgroundResource(R.drawable.bound_green5_shape);
    }
}
